package com.trussell.genesis.dialogs;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import com.trussell.genesis.Genesis;

public class SelectWorkspaceDialog extends JDialog {

	private static final long serialVersionUID = 1L;

	public SelectWorkspaceDialog() {
		super();
		setModal(true);
		setTitle("Workspace Chooser");

		/*
		 * GENERATED BY NETBEANS
		 */
		javax.swing.JLabel selWorkspaceLabel = new javax.swing.JLabel();
		final javax.swing.JTextField textField = new javax.swing.JTextField();
		javax.swing.JButton browseButton = new javax.swing.JButton();
		javax.swing.JButton okButton = new javax.swing.JButton();

		selWorkspaceLabel.setText("Select a Workspace:");
		browseButton.setText("Browse"); // NOI18N
		okButton.setText("OK"); // NOI18N

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																textField,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																326,
																Short.MAX_VALUE)
														.addComponent(
																selWorkspaceLabel)
														.addGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																layout
																		.createSequentialGroup()
																		.addComponent(
																				browseButton)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				okButton)))
										.addContainerGap()));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(selWorkspaceLabel)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												textField,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(okButton)
														.addComponent(
																browseButton))
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));

		/*
		 * Enables file browsing
		 */
		browseButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser jfc = new JFileChooser();
				jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				jfc.showOpenDialog(Genesis.getInstance());
				textField.setText(jfc.getSelectedFile().getPath());
			}
		});

		/*
		 * Creates directory.
		 */
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				String myDir = textField.getText() + "/";
				File dirFile = new File(myDir);

				if (!dirFile.exists()) {
					int choice = JOptionPane.showConfirmDialog(null,
							"Directory doesn't exist.\nCreate?");
					if (choice == JOptionPane.YES_OPTION) {
						dirFile.mkdir();
						buildWorkspace(dirFile);
						Genesis.getInstance().setWorkspace(dirFile);
					}
				} else {
					if (hasIntegrity(dirFile)) {
						Genesis.getInstance().setWorkspace(dirFile);
					} else {
						int choice = JOptionPane.showConfirmDialog(null,
								"Directory corrupted.\nRecreate?");
						if (choice == JOptionPane.YES_OPTION) {
							buildWorkspace(dirFile);
							Genesis.getInstance().setWorkspace(dirFile);
						}
					}
				}
				dispose();
			}
		});

		pack();
		setLocationRelativeTo(null);
		setVisible(true);
	}

	private void buildWorkspace(File topDir) {
		String myDir = topDir.getPath() + "/";
		File assetsFolder = new File(myDir + "assets");
		File textures = new File(myDir + "assets/textures");
		File models = new File(myDir + "assets/models");
		File scripts = new File(myDir + "assets/scripts");
		File compiled = new File(myDir + "assets/compiled");
		File workspaceFile = new File(myDir + "workspace.xml");

		assetsFolder.mkdir();
		textures.mkdir();
		models.mkdir();
		scripts.mkdir();
		compiled.mkdir();

		try {
			workspaceFile.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		dispose();
	}

	public static boolean hasIntegrity(File topDir) {
		if (!topDir.isDirectory())
			return false;
		File[] files = topDir.listFiles();

		boolean hasAssets = false;
		boolean hasTextures = false;
		boolean hasModels = false;
		boolean hasScripts = false;
		boolean hasXML = false;

		for (File f : files) {
			if (f.getPath().endsWith(topDir.getPath() + "\\assets"))
				hasAssets = true;
			if (f.getPath().endsWith(topDir.getPath() + "\\workspace.xml"))
				hasXML = true;
		}
		
		if(hasAssets) {
			File[] assetFolders = new File(topDir.getPath() + "/assets").listFiles();
			for(File f : assetFolders) {
				if (f.getPath().endsWith(topDir.getPath() + "\\assets\\textures"))
					hasTextures = true;
				if (f.getPath().endsWith(topDir.getPath() + "\\assets\\models"))
					hasModels = true;
				if (f.getPath().endsWith(topDir.getPath() + "\\assets\\scripts"))
					hasScripts = true;
			}
		}

		if (hasAssets && hasTextures && hasModels && hasScripts && hasXML)
			return true;
		return false;
	}
}
